
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:im_base/public/global/global.dart';

import '../../logic.dart';
import 'logic.dart';

class AudioWidget extends StatelessWidget {
  final bool isSelf;
  final String msgId;
  final String url;
  final String path;
  final int timeLength;
  final String? chatId;
  final AudioLogic logic = Get.put(AudioLogic());

  AudioWidget(this.msgId, this.path, this.url, this.timeLength, this.isSelf,this.chatId);

  @override
  Widget build(BuildContext context) {

    return Container(
      key: Key(msgId),
      padding: EdgeInsets.fromLTRB(16, 12, 16, 12),
      margin: isSelf
          ? EdgeInsets.only(top: 12, right: 8)
          : EdgeInsets.only(top: 12, left: 8),
      decoration: BoxDecoration(
        color: isSelf ? Colors.green[300] : Colors.white,
        border: Border.all(color: Colors.grey[200]!, width: 0.5),
        borderRadius: isSelf
            ? BorderRadius.only(
                topLeft: Radius.circular(6),
                bottomLeft: Radius.circular(6),
                bottomRight: Radius.circular(24))
            : BorderRadius.only(
                topRight: Radius.circular(6),
                bottomRight: Radius.circular(6),
                bottomLeft: Radius.circular(24)),
      ),
      width: 120,
      height: 45,
      child: GetBuilder<AudioLogic>(
          init: logic,
          builder: (logic) {
            return InkWell(
                onTap: () {
                  if (logic.isPlaying(msgId)) {
                    Get.find<ChatLogic>(tag: chatId).stopAudio(url, path, msgId);
                  } else {
                    Get.find<ChatLogic>(tag: chatId).playAudio(url, path, msgId);
                  }
                },
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    Container(
                      margin: EdgeInsets.only(left: 6),
                      height: 25,
                      child: logic.isPlaying(msgId)
                          ? Image.asset(
                              '${Global.IMAGE_PATH}${logic.state.playImage}',
                              color: Colors.blueAccent,
                            )
                          : Image.asset(
                              '${Global.IMAGE_PATH}${logic.state.stopImage}',
                              color: Colors.blueAccent,
                            ),
                    ),
                    Text(
                      '${timeLength}s',
                      style: TextStyle(color: Colors.black87, fontSize: 14),
                    )
                  ],
                ));
          }),
    );
  }
}
